<?php 
include 'mysql.php';
error_reporting(E_ERROR);
	
function pref($a,$b){
	$quest_a=array();
	$quest_b=array();
	
	$result= select_macth_quest($a,$b);
	//echo mysql_error();
	while($row=mysql_fetch_array($result)){
		$quest_a[$row['idquestion']]=$row['level'];
	}
	$result= select_macth_quest($b,$a);
	//echo mysql_error();
	while($row=mysql_fetch_array($result)){
		$quest_b[$row['idquestion']]=$row['level'];
	}
	$sum=0;
	foreach($quest_a as $key=>$vl){
		$a_vl=$vl;
		$b_vl=$quest_b[$key];
		$sum+=point($a_vl, $b_vl);	
	}
	$sum_vl_b=select_sum_level($b);
	$pref=round(($sum/$sum_vl_b)*100);
	//echo 'pref(1,8)='.$sum.'/'.$sum_vl_b.'='.$pref;
	return $pref;
}

function point($l_ans_a, $l_ans_b){
	$max=$l_ans_a>$l_ans_b?$l_ans_a:(($l_ans_a==$l_ans_b)?0:$l_ans_b);
	$min=$l_ans_a<$l_ans_b?$l_ans_a:$l_ans_b;
	return ($l_ans_b-($max/$min));
}

function update_pref($iduser, $user_aim){
	echo 'update_pref()<br/>';
	$p_u=pref($iduser, $user_aim);
	update_pref_db($iduser, $user_aim, $p_u);
	echo mysql_error();
	$p_aim=pref($user_aim, $iduser);
	update_pref_db($user_aim, $iduser, $p_aim);
	echo mysql_error();
	echo 'end update_pref()<br/>';
}

function build_list4match($sex){
	echo 'build_list4match()<br/>';
	$list=array(
						"iduser"=>0,
						"status"=>0,// NULL=single, !NULL=idSO
						"u_aim"=>array(
									"iduser"=>0,// !NULL=idSO
								),
					);
	//$list=array();
	$result=select_user4sex($sex);
	echo mysql_error();
	$i=0;
	//build...
	while($row=mysql_fetch_array($result)){
		$i++;
		$list[$i]["iduser"]=$row["user"];
		$list[$i]["status"]=0;

		$re_aim=select_aim_user($row["user"]);
		echo mysql_error();
		$j=0;
		while($row_aim=mysql_fetch_array($re_aim)){
				$j++;
				$list[$i]["u_aim"][$j]["iduser"]=$row_aim['user_aim'];
				//echo 'row';
//				print_r('row'.$row_aim);
		}
	}
	//if($sex==2){
//		$_GLOBALS["num_male"]=$i;
//	}elseif($sex==1){
//		$_GLOBALS["num_fmale"]=$i;
//	}
	//print_r($list);
	echo 'end build_list4match()<br/>';
	return $list;
}

function search_single($array){
	echo 'search_single()<br/>';
	foreach($array as $men){
		
		if(!($men["status"])){
			if($men["iduser"]) {
				echo (int)$men["iduser"].'- status: '.$men["status"].'<br/>';
				return (int)$men["iduser"];
			}
		}
	}
	echo 'end search_single()<br/>';
	return 0;
}

function stableMatching(){
	
	$male=array();
    $female=array();
//    Khởi tạo m ∈ M và w ∈ W bằng độc thân 
	$male=build_list4match(2);//sex=2 ~= male;
	echo 'builed male list:<br/>';
// 	//print_r($male);
// 	foreach($male as $u){
// 		echo 'user '.$u['iduser'].': status '.$u["status"];
// 		echo "aim:";
// 		foreach($u["u_aim"] as $aim){
// 			echo $aim["iduser"];echo '-';
// 		}
// 		echo '<br/>';
		
// 	}echo 'END male list<br/>';
// 	//print_r($male);
	$female=build_list4match(1);//sex=1 ~= female;
	echo 'builed female list:<br/>';
// 	//print_r($female);
// 	foreach($female as $u){
// 		echo 'user '.$u['iduser'].': status '.$u["status"];
// 		echo "aim:";
// 		foreach($u["u_aim"] as $aim){
// 			echo $aim["iduser"];echo '-';
// 		}
// 		echo '<br/>';
// 	}echo 'End female list<br/>';
// 	echo '<br/>';
// 	//print_r($female);

	$single=0;
	$single=search_single($male);
	echo 'single:';echo $single;
	while($single){
		echo 'while_begin:<br/>';
		foreach($male as &$male_user){//foreach 1
		echo '<br/>duyet danh sach male';
			if((int)$male_user["iduser"])
				$this_men=(int)$male_user["iduser"];//neu k co $single
			if($single && ($male_user["iduser"]==$single)){//tim ra chang trai doc than dau tien
				$this_men=$single;
			}
			echo '<br/> tim ban gai cho male '.$this_men.'<br/>';
			if(!($male_user["status"])){
				//echo 'find:'; echo $find=true;// tiep tuc tim kiem cac co gai trong danh sach
				echo '<br/>';
				//foreach 2
				foreach($male_user["u_aim"] as &$u_aim){	//danh sach cac co gai cua chang trai nay						
					echo 'ban gai ';
					echo $aim=$u_aim["iduser"];// luu id cua ng con gai nay lai
					echo '<br/>';
					$frch_fmale=0;
					foreach($female as &$fmale_user){//xem qua tung co gai
													// de tham chieu den co gai do (foreach 3)
						
						if($fmale_user["iduser"]==$aim){//tim ra ng con gai do
						echo 'found female '.$fmale_user["iduser"].',status :'.$fmale_user["status"] ;
							
							if(empty($fmale_user["status"])){//neu chua dinh hon
								echo 'single, ';
								echo 'men married with: ';
								echo $male_user["status"]=$u_aim["iduser"];//chang trai da dinh hon voi co gai
								echo 'female married with: ';
								$fmale_user["status"]=(int)$this_men;//co gai da dinh hon voi chang trai
								echo $fmale_user["status"];
								$frch_fmale=1;
							}elseif($fmale_user["status"]){//neu da  dinh hon
								echo 'but married with ';
								$new_men=$this_men;
								echo (int)$old_men=$fmale_user["status"];echo ':))';
								foreach($fmale_user["u_aim"] as $level=>$men){//(foreach 4)
									if($men["iduser"]==$new_men){
										echo ' this_men-level: '.$new_men=$level;//muc yeu thich cua female doi voi new_men
									}
									if($men["iduser"]==$old_men){
										echo ' old men :'.$idold_men=$old_men;
										echo', old level: '.$old_men=$level;//muc yeu thich cua female doi voi old_men
									}
										
								}// end foreach 4
								echo 'compare...';
								if($new_men<$old_men){// neu ng do thich new_men hon old_men
									echo 'changed, new men:'; 
									echo $female_user["status"]=$new_men;
									$male_user["status"]=$aim;
									//echo '('.$new_men.'-'.$aim.')';
									foreach($male as &$single_men){//old_men tro thanh doc than
										if($single_men["iduser"]==$idold_men)
											$single_men["status"]=0;
									}
									$frch_fmale=1;
								}else echo './.k thay doi ';
							}//end else
							echo "xong ";
							//dung tim trong danh sach  female cac co gai cua chang trai doc than 
						}//end if($fmale_user["iduser"]==$aim)
					}//end foreach 3
					echo '<br/>';if($frch_fmale) {echo 'dung'; break;}
					// dung tim danh sach cac co gai.
				}//end foreach 2
			}//end if($male_user["status"]==NULL)
			echo '<br/>';
				foreach($male as $u){
			echo 'user '.$u['iduser'].': status '.$u["status"];
			echo "aim:";
			foreach($u["u_aim"] as $aim){
				echo $aim["iduser"];echo '-';
			}
			echo '<br/>';
				}
	
		}//end foreach 1
	
		$single=search_single($male);
	}//end while

	echo 'update...<br/>';
	if(update_match_db($male))echo 'updated male<br/>';
	//reset($female);
	if(update_match_db($female)) echo 'updated female<br/>';
	echo 'end';
	
	

//	print_r($male);
//	print_r($female);
}//end stableMatching()	
//    Khá»Ÿi táº¡o m âˆˆ M vÃ  w âˆˆ W báº±ng Ä‘á»™c thÃ¢n	
//    while âˆƒ ngÆ°á»�i Ä‘Ã n Ã´ng Ä‘á»™c thÃ¢n m váº«n cÃ²n cÃ³ ngÆ°á»�i phá»¥ ná»¯ w Ä‘á»ƒ cáº§u hÃ´n {
//       w = ngÆ°á»�i phá»¥ ná»¯ m thÃ­ch nháº¥t mÃ  váº«n chÆ°a cáº§u hÃ´n
//       if w Ä‘á»™c thÃ¢n
//         (m, w) trá»Ÿ thÃ nh Ä‘Ã£ Ä‘Ã­nh hÃ´n
//       else má»™t cáº·p (m', w) Ä‘Ã£ Ä‘Ã­nh hÃ´n
//         if w thÃ­ch m to m'
//           (m, w) trá»Ÿ thÃ nh Ä‘Ã£ Ä‘Ã­nh hÃ´n
//           m' trá»Ÿ thÃ nh Ä‘á»™c thÃ¢n
//         else
//           (m', w) váº«n Ä‘Ã£ Ä‘Ã­nh hÃ´n
//    

//
//error_reporting(E_ERROR);
//$r=array(
//		"iduser"=>"",
//		"sta"=>"",
//		"aim"=>array(
//					"iduser"=>"",
//					"sta"=>"",
//				),
//	);
//for($i=0; $i<3; $i++){
//	echo $r[$i]['iduser']='user'.$i; echo '_____';
//	echo $r[$i]["sta"]='sta'.$i;echo '<br/>';
//for($j="a"; $j<"e"; $j++){
//	echo 'id_aim: '.$r[$i]["aim"][$j]['iduser']=$j;
//	echo ' status: '.$r[$i]["aim"][$j]['sta']=$i.'-'.$j; echo '<br/>';
//}
//}
//
//
//echo '/////////////<br/>';
////
////foreach($r as &$user){
////	if($user['iduser']){
////		$user['iduser']='we';
////		$user['sta']='re';
////		
////		foreach($user['aim'] as &$aim){
////			$aim['iduser']=45; 
////			$aim['sta']=NULL;	
////		}	
////	}
////}
//$r[1]["sta"]=NULL;
//echo '\\\\\\\\\\\\\\\\\\\\\\\\\\\<br/>';
//foreach($r as $user){
//	if($user['iduser']){
//	echo 'user: '.$user['iduser'].'________status: '.$user['sta'].'<br/> ';
//	;
//	foreach($user['aim'] as $k=>$aim){
//		echo 'user_aim_no: '.$k.': '.$aim['iduser'].'__ status: '.$aim['sta'].'<br/>';	
//	}	
//	}
//}

?>